package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.CommonDict;
import entities.TWebsite;
import interfaces.IWebsiteService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class WebsiteAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IWebsiteService websiteService;

	private TWebsite websiteBean;
	private List<TWebsite> websiteList;

	private String merNo;
	private String webNo;
	private String webNos;
	private String webSign;
	private String webAddress;
	private String webDescript;
	private String webOwner;
	private String webRegister;
	private Double webValidity;
	private String webState;
	private Date webDate;
	private Date webDateTop;
	private Date webDateEnd;
	private String webVstate;
	private Date webVdate;
	private Date webVdateTop;
	private Date webVdateEnd;
	private String webRemark;

	// 展示对象
	public String show() throws Exception {
		if (!websiteService.exist(webNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			websiteBean = websiteService.fetch(webNo);
			return SUCCESS;
		}
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();
		
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("webDefault",ActionContext.getContext().getSession().get("query").toString());
		paramMap.put("webNo", StringUtil.split(webNo));
		paramMap.put("webSign", webSign);
		paramMap.put("webAddress", webAddress);
		paramMap.put("webDescript", webDescript);
		paramMap.put("webOwner", webOwner);
		paramMap.put("webRegister", webRegister);
		paramMap.put("webValidity", webValidity);
		paramMap.put("webState", webState);
		paramMap.put("webDate", ParameterUtil.dateCombin(webDateTop, webDateEnd));
		paramMap.put("webVstate", webVstate);
		paramMap.put("webVdate", ParameterUtil.dateCombin(webVdateTop, webVdateEnd));
		paramMap.put("webRemark", webRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "webDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TWebsite> resultSet = websiteService.search(querySet);
		websiteList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}
	
	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("webDefault",ActionContext.getContext().getSession().get("query").toString());
		paramMap.put("webNo", StringUtil.split(webNo));
		paramMap.put("webSign", webSign);
		paramMap.put("webAddress", webAddress);
		paramMap.put("webDescript", webDescript);
		paramMap.put("webOwner", webOwner);
		paramMap.put("webRegister", webRegister);
		paramMap.put("webValidity", webValidity);
		paramMap.put("webState", webState);
		paramMap.put("webDate", ParameterUtil.dateCombin(webDateTop, webDateEnd));
		paramMap.put("webVstate", webVstate);
		paramMap.put("webVdate", ParameterUtil.dateCombin(webVdateTop, webVdateEnd));
		paramMap.put("webRemark", webRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "webDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TWebsite> resultSet = websiteService.search(querySet);
		websiteList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("网站标识");
		fieldHead.add("网站地址");
		fieldHead.add("网站描述");
		fieldHead.add("网站所属人");
		fieldHead.add("网站注册日期");
		fieldHead.add("有效期");
		fieldHead.add("是否可用");
		fieldHead.add("创建日期日期");
		fieldHead.add("创建日期时间");
		fieldHead.add("审核状态");
		fieldHead.add("审核日期日期");
		fieldHead.add("审核日期时间");
		fieldHead.add("网站备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TWebsite websiteBean : websiteList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(websiteBean.getWebNo());
			fieldItem.add(websiteBean.getWebSign());
			fieldItem.add(websiteBean.getWebAddress());
			fieldItem.add(websiteBean.getWebDescript());
			fieldItem.add(websiteBean.getWebOwner());
			fieldItem.add(websiteBean.getWebRegister());
			fieldItem.add(websiteBean.getWebValidity().toString());
			fieldItem.add((websiteBean.getWebState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(websiteBean.getWebDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(websiteBean.getWebDate(), DateUtil.TIME));
			fieldItem.add((websiteBean.getWebVstate().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(websiteBean.getWebVdate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(websiteBean.getWebVdate(), DateUtil.TIME));
			fieldItem.add(websiteBean.getWebRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IWebsiteService getWebsiteService() {
		return websiteService;
	}

	public void setWebsiteService(IWebsiteService websiteService) {
		this.websiteService = websiteService;
	}

	public TWebsite getWebsiteBean() {
		return websiteBean;
	}

	public void setWebsiteBean(TWebsite websiteBean) {
		this.websiteBean = websiteBean;
	}

	public List<TWebsite> getWebsiteList() {
		return websiteList;
	}

	public void setWebsiteList(List<TWebsite> websiteList) {
		this.websiteList = websiteList;
	}

	public String getWebNo() {
		return webNo;
	}

	public void setWebNo(String webNo) {
		this.webNo = webNo;
	}

	public String getWebNos() {
		return webNos;
	}

	public void setWebNos(String webNos) {
		this.webNos = webNos;
	}

	public String getWebSign() {
		return webSign;
	}

	public void setWebSign(String webSign) {
		this.webSign = webSign;
	}

	public String getWebAddress() {
		return webAddress;
	}

	public void setWebAddress(String webAddress) {
		this.webAddress = webAddress;
	}

	public String getWebDescript() {
		return webDescript;
	}

	public void setWebDescript(String webDescript) {
		this.webDescript = webDescript;
	}

	public String getWebState() {
		return webState;
	}

	public void setWebState(String webState) {
		this.webState = webState;
	}

	public Date getWebDate() {
		return webDate;
	}

	public void setWebDate(Date webDate) {
		this.webDate = webDate;
	}

	public Date getWebDateTop() {
		return webDateTop;
	}

	public void setWebDateTop(Date webDateTop) {
		this.webDateTop = webDateTop;
	}

	public Date getWebDateEnd() {
		return webDateEnd;
	}

	public void setWebDateEnd(Date webDateEnd) {
		this.webDateEnd = webDateEnd;
	}

	public String getWebVstate() {
		return webVstate;
	}

	public void setWebVstate(String webVstate) {
		this.webVstate = webVstate;
	}

	public Date getWebVdate() {
		return webVdate;
	}

	public void setWebVdate(Date webVdate) {
		this.webVdate = webVdate;
	}

	public Date getWebVdateTop() {
		return webVdateTop;
	}

	public void setWebVdateTop(Date webVdateTop) {
		this.webVdateTop = webVdateTop;
	}

	public Date getWebVdateEnd() {
		return webVdateEnd;
	}

	public void setWebVdateEnd(Date webVdateEnd) {
		this.webVdateEnd = webVdateEnd;
	}

	public String getWebRemark() {
		return webRemark;
	}

	public void setWebRemark(String webRemark) {
		this.webRemark = webRemark;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getWebOwner() {
		return webOwner;
	}

	public void setWebOwner(String webOwner) {
		this.webOwner = webOwner;
	}

	public String getWebRegister() {
		return webRegister;
	}

	public void setWebRegister(String webRegister) {
		this.webRegister = webRegister;
	}

	public Double getWebValidity() {
		return webValidity;
	}

	public void setWebValidity(Double webValidity) {
		this.webValidity = webValidity;
	}

	
	
}
